<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>闭包 | Swift 简介</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.1.0">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
    

        
    
    
    <link rel="next" href="./10-init_func.html" />
    
    
    <link rel="prev" href="./08-func.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="0.9" data-basepath="." data-revision="Sun Jul 26 2015 06:31:06 GMT+0800 (CST)">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
        
        
        

        

        
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                        <i class="fa fa-check"></i>
                        
                        Swift快速入门
                    </a>
                
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="0.1" data-path="01-playground.html">
            
                
                    <a href="./01-playground.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.1.</b>
                        
                        Playground
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.2" data-path="02-var_and_let.html">
            
                
                    <a href="./02-var_and_let.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.2.</b>
                        
                        变量和常量
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.3" data-path="03-optional.html">
            
                
                    <a href="./03-optional.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.</b>
                        
                        Optional可选值
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.4" data-path="04-control_flow.html">
            
                
                    <a href="./04-control_flow.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.</b>
                        
                        控制流
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.5" data-path="05-loop.html">
            
                
                    <a href="./05-loop.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.5.</b>
                        
                        循环
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.6" data-path="06-string.html">
            
                
                    <a href="./06-string.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.</b>
                        
                        字符串
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.7" data-path="07-collection.html">
            
                
                    <a href="./07-collection.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.7.</b>
                        
                        集合
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.8" data-path="08-func.html">
            
                
                    <a href="./08-func.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.8.</b>
                        
                        函数
                    </a>
                
            
            
        </li>
    
        <li class="chapter active" data-level="0.9" data-path="09-closure.html">
            
                
                    <a href="./09-closure.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.9.</b>
                        
                        闭包
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.10" data-path="10-init_func.html">
            
                
                    <a href="./10-init_func.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.10.</b>
                        
                        构造函数
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.11" data-path="11-lazy.html">
            
                
                    <a href="./11-lazy.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.11.</b>
                        
                        懒加载 &amp; didSet
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.12" data-path="12-ats.html">
            
                
                    <a href="./12-ats.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.12.</b>
                        
                        ATS
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                Published with GitBook
            </a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    Share on Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    Share on Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    Share on Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    Share on Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    Share on Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Swift 简介</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="%E9%97%AD%E5%8C%85">&#x95ED;&#x5305;</h1>
<ul>
<li>&#x95ED;&#x5305;&#x7C7B;&#x4F3C;&#x4E8E; OC &#x4E2D;&#x7684; Block<ul>
<li>&#x9884;&#x5148;&#x5B9A;&#x4E49;&#x597D;&#x7684;&#x4EE3;&#x7801;</li>
<li>&#x5728;&#x9700;&#x8981;&#x65F6;&#x6267;&#x884C;</li>
<li>&#x53EF;&#x4EE5;&#x5F53;&#x4F5C;&#x53C2;&#x6570;&#x4F20;&#x9012;</li>
<li>&#x53EF;&#x4EE5;&#x6709;&#x8FD4;&#x56DE;&#x503C;</li>
<li>&#x5305;&#x542B; <code>self</code> &#x65F6;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x5FAA;&#x73AF;&#x5F15;&#x7528;</li>
</ul>
</li>
</ul>
<h2 id="%E5%AE%9A%E4%B9%89">&#x5B9A;&#x4E49;</h2>
<ul>
<li>&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x51FD;&#x6570;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-comment">//: &#x5B9A;&#x4E49;&#x4E00;&#x4E2A; sum &#x51FD;&#x6570;</span>
<span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">sum</span><span class="hljs-params">(num1 num1: Int, num2: Int)</span> -&gt; <span class="hljs-title">Int</span> </span>{
    <span class="hljs-keyword">return</span> num1 + num2
}
sum(num1: <span class="hljs-number">10</span>, num2: <span class="hljs-number">30</span>)

<span class="hljs-comment">//: &#x5728; Swift &#x4E2D;&#x51FD;&#x6570;&#x672C;&#x8EAB;&#x5C31;&#x53EF;&#x4EE5;&#x5F53;&#x4F5C;&#x53C2;&#x6570;&#x88AB;&#x5B9A;&#x4E49;&#x548C;&#x4F20;&#x9012;</span>
<span class="hljs-keyword">let</span> mySum = sum
<span class="hljs-keyword">let</span> result = mySum(num1: <span class="hljs-number">20</span>, num2: <span class="hljs-number">30</span>)
</code></pre>
<ul>
<li>&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x95ED;&#x5305;<ul>
<li>&#x95ED;&#x5305;: &#x95ED;&#x5305;&#x7C7B;&#x578B; = { (&#x884C;&#x53C2;) -&gt; &#x8FD4;&#x56DE;&#x503C; in // &#x4EE3;&#x7801;&#x5B9E;&#x73B0; }</li>
<li><code>in</code> &#x7528;&#x4E8E;&#x533A;&#x5206;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x548C;&#x4EE3;&#x7801;&#x5B9E;&#x73B0;</li>
</ul>
</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-comment">//: &#x95ED;&#x5305;: &#x95ED;&#x5305;&#x7C7B;&#x578B; = { (&#x884C;&#x53C2;) -&gt; &#x8FD4;&#x56DE;&#x503C; in // &#x4EE3;&#x7801;&#x5B9E;&#x73B0; }</span>
<span class="hljs-keyword">let</span> sumFunc: (num1: <span class="hljs-type">Int</span>, num2: <span class="hljs-type">Int</span>) -&gt; <span class="hljs-type">Int</span> = { (x, y) -&gt; <span class="hljs-type">Int</span> <span class="hljs-keyword">in</span>
    <span class="hljs-keyword">return</span> x + y
}

sumFunc(num1: <span class="hljs-number">10</span>, num2: <span class="hljs-number">50</span>)
</code></pre>
<h2 id="%E9%97%AD%E5%8C%85%E5%AE%9E%E6%88%98">&#x95ED;&#x5305;&#x5B9E;&#x6218;</h2>
<h3 id="gcd-%E5%BC%82%E6%AD%A5">GCD &#x5F02;&#x6B65;</h3>
<ul>
<li>&#x6A21;&#x62DF;&#x5728;&#x540E;&#x53F0;&#x7EBF;&#x7A0B;&#x52A0;&#x8F7D;&#x6570;&#x636E;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">loadData</span><span class="hljs-params">()</span> </span>{
    dispatch_async(dispatch_get_global_queue(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>), { () -&gt; <span class="hljs-type">Void</span> <span class="hljs-keyword">in</span>
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;&#x8017;&#x65F6;&#x64CD;&#x4F5C; <span class="hljs-subst">\(NSThread .currentThread()</span>)&quot;</span>)
    })
}
</code></pre>
<ul>
<li>&#x5C3E;&#x968F;&#x95ED;&#x5305;&#xFF0C;&#x5982;&#x679C;&#x95ED;&#x5305;&#x662F;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x4EE5;&#x4E0B;&#x5199;&#x6CD5;</li>
<li>&#x6CE8;&#x610F;&#x4E0A;&#x4E0B;&#x4E24;&#x6BB5;&#x4EE3;&#x7801;&#xFF0C;<code>}</code> &#x7684;&#x4F4D;&#x7F6E;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">loadData</span><span class="hljs-params">()</span> </span>{
    dispatch_async(dispatch_get_global_queue(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)) { () -&gt; <span class="hljs-type">Void</span> <span class="hljs-keyword">in</span>
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;&#x8017;&#x65F6;&#x64CD;&#x4F5C; <span class="hljs-subst">\(NSThread .currentThread()</span>)&quot;</span>)
    }
}
</code></pre>
<ul>
<li>&#x95ED;&#x5305;&#x7684;&#x7B80;&#x5199;&#xFF0C;&#x5982;&#x679C;&#x95ED;&#x5305;&#x4E2D;&#x6CA1;&#x6709;&#x53C2;&#x6570;&#x548C;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x53EF;&#x4EE5;&#x7701;&#x7565;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">loadData</span><span class="hljs-params">()</span> </span>{
    dispatch_async(dispatch_get_global_queue(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)) {
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;&#x8017;&#x65F6;&#x64CD;&#x4F5C; <span class="hljs-subst">\(NSThread .currentThread()</span>)&quot;</span>)
    }
}
</code></pre>
<h3 id="%E8%87%AA%E5%AE%9A%E4%B9%89%E9%97%AD%E5%8C%85%E5%8F%82%E6%95%B0%EF%BC%8C%E5%AE%9E%E7%8E%B0%E4%B8%BB%E7%BA%BF%E7%A8%8B%E5%9B%9E%E8%B0%83">&#x81EA;&#x5B9A;&#x4E49;&#x95ED;&#x5305;&#x53C2;&#x6570;&#xFF0C;&#x5B9E;&#x73B0;&#x4E3B;&#x7EBF;&#x7A0B;&#x56DE;&#x8C03;</h3>
<ul>
<li>&#x6DFB;&#x52A0;&#x6CA1;&#x6709;&#x53C2;&#x6570;&#xFF0C;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x95ED;&#x5305;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-keyword">override</span> <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">viewDidLoad</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">super</span>.viewDidLoad()

    loadData {
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;&#x5B8C;&#x6210;&#x4E86; <span class="hljs-subst">\(NSThread .currentThread()</span>)&quot;</span>)
    }
}

<span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">loadData</span><span class="hljs-params">(finished: <span class="hljs-params">()</span> -&gt; <span class="hljs-params">()</span>)</span> </span>{
    dispatch_async(dispatch_get_global_queue(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)) {
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;&#x8017;&#x65F6;&#x64CD;&#x4F5C; <span class="hljs-subst">\(NSThread .currentThread()</span>)&quot;</span>)

        dispatch_async(dispatch_get_main_queue()) {
            <span class="hljs-comment">// &#x6267;&#x884C;&#x56DE;&#x8C03;</span>
            finished()
        }
    }
}
</code></pre>
<ul>
<li>&#x6DFB;&#x52A0;&#x56DE;&#x8C03;&#x53C2;&#x6570;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-keyword">override</span> <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">viewDidLoad</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">super</span>.viewDidLoad()

    loadData { message <span class="hljs-keyword">in</span>
        <span class="hljs-built_in">print</span>(message + <span class="hljs-string">&quot; <span class="hljs-subst">\(NSThread .currentThread()</span>)&quot;</span>)
    }
}

<span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">loadData</span><span class="hljs-params">(finished: <span class="hljs-params">(msg: String)</span> -&gt; <span class="hljs-params">()</span>)</span> </span>{
    dispatch_async(dispatch_get_global_queue(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)) {
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;&#x8017;&#x65F6;&#x64CD;&#x4F5C; <span class="hljs-subst">\(NSThread .currentThread()</span>)&quot;</span>)

        dispatch_async(dispatch_get_main_queue()) {
            <span class="hljs-comment">// &#x6267;&#x884C;&#x56DE;&#x8C03;</span>
            finished(msg: <span class="hljs-string">&quot;over&quot;</span>)
        }
    }
}
</code></pre>
<h3 id="%E9%97%AD%E5%8C%85%E7%9A%84%E8%BF%94%E5%9B%9E%E5%80%BC%E6%BC%94%E7%BB%83">&#x95ED;&#x5305;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x6F14;&#x7EC3;</h3>
<blockquote>
<p>&#x76EE;&#x6807;&#xFF1A;&#x7528;&#x95ED;&#x5305;&#x5B9E;&#x73B0;<code>&#x7F51;&#x6613;&#x65B0;&#x95FB;</code>&#x4E2D;&#x6EDA;&#x52A8;&#x6807;&#x7B7E;&#x89C6;&#x56FE;</p>
</blockquote>
<ul>
<li>&#x5B9E;&#x73B0; <code>scrollView</code> &#x51FD;&#x6570;&#xFF0C;&#x8FD4;&#x56DE; <code>UIScrollView</code></li>
</ul>
<pre><code class="lang-swift"><span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">scrollView</span><span class="hljs-params">(frame: CGRect, numbersOfLabel: <span class="hljs-params">()</span>-&gt; Int, labelForIndex: <span class="hljs-params">(index: Int)</span> -&gt; UILabel)</span> -&gt; <span class="hljs-title">UIScrollView</span> </span>{
    <span class="hljs-comment">// 1. &#x5B9E;&#x4F8B;&#x5316; scrollView</span>
    <span class="hljs-keyword">let</span> sv = <span class="hljs-type">UIScrollView</span>(frame: frame)

    <span class="hljs-comment">// 2. label &#x7684;&#x6570;&#x91CF;</span>
    <span class="hljs-keyword">let</span> <span class="hljs-built_in">count</span> = numbersOfLabel()
    <span class="hljs-built_in">print</span>(<span class="hljs-built_in">count</span>)

    <span class="hljs-comment">// 3. &#x5FAA;&#x73AF;&#x521B;&#x5EFA; label &#x7684;&#x5185;&#x5BB9;</span>
    <span class="hljs-keyword">let</span> margin: <span class="hljs-type">CGFloat</span> = <span class="hljs-number">8</span>
    <span class="hljs-keyword">var</span> x = margin

    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span>..&lt;<span class="hljs-built_in">count</span> {
        <span class="hljs-keyword">let</span> label = labelForIndex(index: i)
        label.frame = <span class="hljs-type">CGRect</span>(x: x, y: <span class="hljs-number">0</span>, width: label.bounds.width, height: frame.height)

        sv.addSubview(label)

        <span class="hljs-comment">// &#x7D2F;&#x52A0; x &#x5750;&#x6807;</span>
        x += label.bounds.width
    }

    <span class="hljs-comment">// 4. &#x8BBE;&#x7F6E; scrollView &#x7684; contentSize</span>
    sv.contentSize = <span class="hljs-type">CGSize</span>(width: x, height: frame.height)

    <span class="hljs-comment">// 5. &#x8FD4;&#x56DE;</span>
    <span class="hljs-keyword">return</span> sv
}
</code></pre>
<ul>
<li>&#x51FD;&#x6570;&#x8C03;&#x7528;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-keyword">override</span> <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">viewDidLoad</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">super</span>.viewDidLoad()

    <span class="hljs-keyword">let</span> size = <span class="hljs-type">UIScreen</span>.mainScreen().bounds.size
    <span class="hljs-keyword">let</span> rect = <span class="hljs-type">CGRect</span>(x: <span class="hljs-number">0</span>, y: <span class="hljs-number">20</span>, width: size.width, height: <span class="hljs-number">44</span>)
    view.addSubview(scrollView(rect, numbersOfLabel: { () -&gt; <span class="hljs-type">Int</span> <span class="hljs-keyword">in</span>
        <span class="hljs-keyword">return</span> <span class="hljs-number">10</span>
        }) { (index) -&gt; <span class="hljs-type">UILabel</span> <span class="hljs-keyword">in</span>
            <span class="hljs-comment">// 1. &#x5B9E;&#x4F8B;&#x5316; label</span>
            <span class="hljs-keyword">let</span> label = <span class="hljs-type">UILabel</span>()

            <span class="hljs-comment">// 2. &#x8BBE;&#x7F6E;&#x6587;&#x672C;&#x5185;&#x5BB9;</span>
            label.text = <span class="hljs-string">&quot;Hello <span class="hljs-subst">\(index)</span>&quot;</span>
            <span class="hljs-comment">// 2.1 &#x8BBE;&#x7F6E;&#x5B57;&#x4F53; &amp; &#x5BF9;&#x9F50;&#x65B9;&#x5F0F;</span>
            label.font = <span class="hljs-type">UIFont</span>.systemFontOfSize(<span class="hljs-number">18</span>)
            label.textAlignment = <span class="hljs-type">NSTextAlignment</span>.<span class="hljs-type">Center</span>
            <span class="hljs-comment">// 2.2 &#x81EA;&#x52A8;&#x8BBE;&#x7F6E;&#x5927;&#x5C0F;</span>
            label.sizeToFit()
            <span class="hljs-comment">// 2.3 &#x7F29;&#x5C0F;&#x5B57;&#x4F53;</span>
            label.font = <span class="hljs-type">UIFont</span>.systemFontOfSize(<span class="hljs-number">14</span>)

            <span class="hljs-keyword">return</span> label
    })
}
</code></pre>
<blockquote>
<p>&#x672C;&#x6F14;&#x7EC3;&#x4E0D;&#x4EC5;&#x6F14;&#x793A;&#x4E86;&#x95ED;&#x5305;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x5E94;&#x7528;&#xFF0C;&#x540C;&#x65F6;&#x4E5F;&#x6A21;&#x62DF;&#x4E86; <code>TableView</code> &#x7684;&#x6570;&#x636E;&#x6E90;&#x65B9;&#x6CD5;&#x7528;&#x95ED;&#x5305;&#x7684;&#x5B9E;&#x73B0;&#x601D;&#x8DEF;</p>
</blockquote>
<ul>
<li>&#x81EA;&#x5B9A;&#x4E49;&#x89C6;&#x56FE;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ChannelView</span>: <span class="hljs-title">UIScrollView</span> </span>{

    <span class="hljs-keyword">init</span>(frame: <span class="hljs-type">CGRect</span>, numbersOfLabel: ()-&gt; <span class="hljs-type">Int</span>, labelForIndex: (index: <span class="hljs-type">Int</span>) -&gt; <span class="hljs-type">UILabel</span>) {
        <span class="hljs-comment">// 1. &#x5B9E;&#x4F8B;&#x5316; scrollView</span>
        <span class="hljs-keyword">super</span>.<span class="hljs-keyword">init</span>(frame: frame)

        <span class="hljs-comment">// 2. label &#x7684;&#x6570;&#x91CF;</span>
        <span class="hljs-keyword">let</span> <span class="hljs-built_in">count</span> = numbersOfLabel()
        <span class="hljs-built_in">print</span>(<span class="hljs-built_in">count</span>)

        <span class="hljs-comment">// 3. &#x5FAA;&#x73AF;&#x521B;&#x5EFA; label &#x7684;&#x5185;&#x5BB9;</span>
        <span class="hljs-keyword">let</span> margin: <span class="hljs-type">CGFloat</span> = <span class="hljs-number">8</span>
        <span class="hljs-keyword">var</span> x = margin

        <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span>..&lt;<span class="hljs-built_in">count</span> {
            <span class="hljs-keyword">let</span> label = labelForIndex(index: i)
            label.frame = <span class="hljs-type">CGRect</span>(x: x, y: <span class="hljs-number">0</span>, width: label.bounds.width, height: frame.height)

            addSubview(label)

            <span class="hljs-comment">// &#x7D2F;&#x52A0; x &#x5750;&#x6807;</span>
            x += label.bounds.width
        }

        <span class="hljs-comment">// 4. &#x8BBE;&#x7F6E; scrollView &#x7684; contentSize</span>
        contentSize = <span class="hljs-type">CGSize</span>(width: x, height: frame.height)
    }

    required <span class="hljs-keyword">init</span>?(coder aDecoder: <span class="hljs-type">NSCoder</span>) {
        <span class="hljs-keyword">super</span>.<span class="hljs-keyword">init</span>(coder: aDecoder)
    }
}
</code></pre>
<h2 id="%E9%97%AD%E5%8C%85%E7%9A%84%E5%BE%AA%E7%8E%AF%E5%BC%95%E7%94%A8">&#x95ED;&#x5305;&#x7684;&#x5FAA;&#x73AF;&#x5F15;&#x7528;</h2>
<ul>
<li>&#x4EE3;&#x7801;&#x51C6;&#x5907;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-comment">// &#x5B8C;&#x6210;&#x56DE;&#x8C03;&#x5C5E;&#x6027;&#xFF0C;&#x63A7;&#x5236;&#x5668;&#x5BF9;&#x95ED;&#x5305;&#x5F3A;&#x5F15;&#x7528;</span>
<span class="hljs-keyword">var</span> finishedBlock: (() -&gt; ())?

private <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">loadData</span><span class="hljs-params">(finished: <span class="hljs-params">()</span> -&gt; <span class="hljs-params">()</span>)</span> </span>{
    <span class="hljs-comment">// &#x8BB0;&#x5F55;&#x95ED;&#x5305;</span>
    finishedBlock = finished

    working()
}

private <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">working</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-comment">// &#x5982;&#x679C;&#x5C5E;&#x6027;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x4F7F;&#x7528; ! &#x4F1A;&#x5D29;&#x6E83;</span>
    <span class="hljs-comment">// finishedBlock!()</span>

    <span class="hljs-comment">// &#x5982;&#x679C;&#x5C5E;&#x6027;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x5C31;&#x4E0D;&#x6267;&#x884C;</span>
    finishedBlock?()
}
</code></pre>
<ul>
<li>&#x8C03;&#x7528;&#x95ED;&#x5305;&#xFF0C;&#x4E0E; Block &#x4E00;&#x81F4;&#xFF0C;&#x95ED;&#x5305;&#x4F1A;&#x5BF9; <code>self</code> &#x505A;&#x4E00;&#x6B21; copy&#xFF0C;&#x4ECE;&#x800C;&#x8FDB;&#x884C;&#x5F3A;&#x5F15;&#x7528;</li>
</ul>
<pre><code class="lang-swift">loadData {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;<span class="hljs-subst">\(<span class="hljs-keyword">self</span>.view)</span>&quot;</span>)
}
</code></pre>
<ul>
<li>&#x6D4B;&#x8BD5;&#x89C6;&#x56FE;&#x63A7;&#x5236;&#x5668;&#x7684;&#x91CA;&#x653E;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-comment">/// &#x4E0E; OC &#x4E2D;&#x7684; dealloc &#x7C7B;&#x4F3C;&#xFF0C;&#x6CE8;&#x610F;&#x6B64;&#x51FD;&#x6570;&#x6CA1;&#x6709;()</span>
<span class="hljs-keyword">deinit</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;<span class="hljs-subst">\(classForCoder)</span> 88&quot;</span>)
}
</code></pre>
<h3 id="%E8%A7%A3%E9%99%A4%E5%BE%AA%E7%8E%AF%E5%BC%95%E7%94%A8">&#x89E3;&#x9664;&#x5FAA;&#x73AF;&#x5F15;&#x7528;</h3>
<ul>
<li>&#x4E0E; OC &#x7C7B;&#x4F3C;&#x7684;&#x65B9;&#x6CD5;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-keyword">weak</span> <span class="hljs-keyword">var</span> weakSelf = <span class="hljs-keyword">self</span>
loadData {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;<span class="hljs-subst">\(weakSelf?.view)</span>&quot;</span>)
}
</code></pre>
<ul>
<li>Swift &#x63A8;&#x8350;&#x7684;&#x65B9;&#x6CD5;</li>
</ul>
<pre><code class="lang-swift">loadData { [<span class="hljs-keyword">weak</span> <span class="hljs-keyword">self</span>] <span class="hljs-keyword">in</span>
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;<span class="hljs-subst">\(<span class="hljs-keyword">self</span>?.view)</span>&quot;</span>)
}
</code></pre>
<ul>
<li>&#x8FD8;&#x53EF;&#x4EE5;</li>
</ul>
<pre><code class="lang-swift">loadData { [<span class="hljs-keyword">unowned</span> <span class="hljs-keyword">self</span>] <span class="hljs-keyword">in</span>
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;<span class="hljs-subst">\(<span class="hljs-keyword">self</span>.view)</span>&quot;</span>)
}
</code></pre>
<h4 id="%E9%97%AD%E5%8C%85-block-%E7%9A%84%E5%BE%AA%E7%8E%AF%E5%BC%95%E7%94%A8%E5%B0%8F%E7%BB%93">&#x95ED;&#x5305;(Block) &#x7684;&#x5FAA;&#x73AF;&#x5F15;&#x7528;&#x5C0F;&#x7ED3;</h4>
<ul>
<li><p>Swift</p>
<ul>
<li><code>[weak self]</code><ul>
<li><code>self</code>&#x662F;&#x53EF;&#x9009;&#x9879;&#xFF0C;&#x5982;&#x679C;self&#x5DF2;&#x7ECF;&#x88AB;&#x91CA;&#x653E;&#xFF0C;&#x5219;&#x4E3A;<code>nil</code></li>
</ul>
</li>
<li><code>[unowned self]</code><ul>
<li><code>self</code>&#x4E0D;&#x662F;&#x53EF;&#x9009;&#x9879;&#xFF0C;&#x5982;&#x679C;self&#x5DF2;&#x7ECF;&#x88AB;&#x91CA;&#x653E;&#xFF0C;&#x5219;&#x51FA;&#x73B0;<code>&#x91CE;&#x6307;&#x9488;&#x8BBF;&#x95EE;</code></li>
</ul>
</li>
</ul>
</li>
<li><p>Objc</p>
<ul>
<li><code>__weak typeof(self) weakSelf;</code><ul>
<li>&#x5982;&#x679C;<code>self</code>&#x5DF2;&#x7ECF;&#x88AB;&#x91CA;&#x653E;&#xFF0C;&#x5219;&#x4E3A;<code>nil</code></li>
</ul>
</li>
<li><code>__unsafe_unretained typeof(self) weakSelf;</code><ul>
<li>&#x5982;&#x679C;<code>self</code>&#x5DF2;&#x7ECF;&#x88AB;&#x91CA;&#x653E;&#xFF0C;&#x5219;&#x51FA;&#x73B0;&#x91CE;&#x6307;&#x9488;&#x8BBF;&#x95EE;</li>
</ul>
</li>
</ul>
</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./08-func.html" class="navigation navigation-prev " aria-label="Previous page: 函数"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./10-init_func.html" class="navigation navigation-next " aria-label="Next page: 构造函数"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
